package br.com.jk.config;

import br.com.jk.util.Util;

/**
 *
 * @author camilo
 */
public class CfgJDialog extends javax.swing.JDialog {

  private static CfgJDialog dialog;

  public static CfgJDialog getInstance(java.awt.Frame parent, boolean modal) {
    if (dialog == null) {
      dialog = new CfgJDialog(parent, modal);
    }
    return dialog;
  }

  /**
   * Creates new form Config
   */
  private CfgJDialog(java.awt.Frame parent, boolean modal) {
    super(parent, modal);
    initComponents();
    getCfg();
    setLocationRelativeTo(null);
  }

  /**
   * This method is called from within the constructor to initialize the form. WARNING: Do NOT modify this code. The
   * content of this method is always regenerated by the Form Editor.
   */
  @SuppressWarnings("unchecked")
  // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
  private void initComponents() {

    jtpCfg = new javax.swing.JTabbedPane();
    jpBancoDados = new javax.swing.JPanel();
    jLabel1 = new javax.swing.JLabel();
    jcbBase = new javax.swing.JComboBox();
    jLabel2 = new javax.swing.JLabel();
    jtfIp = new javax.swing.JTextField();
    jLabel3 = new javax.swing.JLabel();
    jtfPorta = new javax.swing.JTextField();
    jLabel4 = new javax.swing.JLabel();
    jtfNomeBd = new javax.swing.JTextField();
    jLabel5 = new javax.swing.JLabel();
    jtfLocal = new javax.swing.JTextField();
    jbBuscarLocal = new javax.swing.JButton();
    jLabel6 = new javax.swing.JLabel();
    jtfUser = new javax.swing.JTextField();
    jLabel7 = new javax.swing.JLabel();
    jpfPass = new javax.swing.JPasswordField();
    jPanel1 = new javax.swing.JPanel();
    jLabel9 = new javax.swing.JLabel();
    jtfIpMixdb = new javax.swing.JTextField();
    jpXml = new javax.swing.JPanel();
    jLabel8 = new javax.swing.JLabel();
    jtfLocalXml = new javax.swing.JTextField();
    jButton1 = new javax.swing.JButton();
    jPanel2 = new javax.swing.JPanel();
    jbGravar = new javax.swing.JButton();
    jbCancelar = new javax.swing.JButton();
    jbPadrao = new javax.swing.JButton();

    setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
    setTitle("Configurações");
    setModal(true);
    setResizable(false);

    jtpCfg.setBorder(javax.swing.BorderFactory.createEtchedBorder());

    jLabel1.setText("Base:");

    jcbBase.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "PostgreSQL" }));
    jcbBase.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        jcbBaseActionPerformed(evt);
      }
    });

    jLabel2.setText("IP");

    jLabel3.setText("Porta");

    jLabel4.setText("Nome do BD");

    jLabel5.setText("Local");

    jbBuscarLocal.setText("Buscar");

    jLabel6.setText("Usuário");

    jLabel7.setText("Senha");

    jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("MixDB"));

    jLabel9.setFont(new java.awt.Font("Liberation Sans", 0, 12)); // NOI18N
    jLabel9.setText("Ip");

    jtfIpMixdb.setFont(new java.awt.Font("Liberation Sans", 0, 12)); // NOI18N

    javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
    jPanel1.setLayout(jPanel1Layout);
    jPanel1Layout.setHorizontalGroup(
      jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addGroup(jPanel1Layout.createSequentialGroup()
        .addContainerGap()
        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
          .addComponent(jLabel9)
          .addComponent(jtfIpMixdb, javax.swing.GroupLayout.PREFERRED_SIZE, 203, javax.swing.GroupLayout.PREFERRED_SIZE))
        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
    );
    jPanel1Layout.setVerticalGroup(
      jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addGroup(jPanel1Layout.createSequentialGroup()
        .addComponent(jLabel9)
        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
        .addComponent(jtfIpMixdb, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        .addGap(0, 14, Short.MAX_VALUE))
    );

    javax.swing.GroupLayout jpBancoDadosLayout = new javax.swing.GroupLayout(jpBancoDados);
    jpBancoDados.setLayout(jpBancoDadosLayout);
    jpBancoDadosLayout.setHorizontalGroup(
      jpBancoDadosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addGroup(jpBancoDadosLayout.createSequentialGroup()
        .addContainerGap()
        .addGroup(jpBancoDadosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
          .addGroup(jpBancoDadosLayout.createSequentialGroup()
            .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
          .addGroup(jpBancoDadosLayout.createSequentialGroup()
            .addGroup(jpBancoDadosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
              .addGroup(jpBancoDadosLayout.createSequentialGroup()
                .addGroup(jpBancoDadosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                  .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jpBancoDadosLayout.createSequentialGroup()
                    .addComponent(jLabel1)
                    .addGap(4, 4, 4)
                    .addComponent(jcbBase, javax.swing.GroupLayout.PREFERRED_SIZE, 153, javax.swing.GroupLayout.PREFERRED_SIZE))
                  .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jpBancoDadosLayout.createSequentialGroup()
                    .addComponent(jLabel2)
                    .addGap(110, 110, 110)
                    .addComponent(jLabel3)
                    .addGap(29, 29, 29)
                    .addComponent(jLabel4))
                  .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jpBancoDadosLayout.createSequentialGroup()
                    .addComponent(jtfIp, javax.swing.GroupLayout.PREFERRED_SIZE, 116, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addComponent(jtfPorta, javax.swing.GroupLayout.PREFERRED_SIZE, 62, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addComponent(jtfNomeBd, javax.swing.GroupLayout.PREFERRED_SIZE, 150, javax.swing.GroupLayout.PREFERRED_SIZE))
                  .addComponent(jLabel5, javax.swing.GroupLayout.Alignment.LEADING))
                .addGap(112, 124, Short.MAX_VALUE))
              .addGroup(jpBancoDadosLayout.createSequentialGroup()
                .addGroup(jpBancoDadosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                  .addComponent(jtfLocal, javax.swing.GroupLayout.DEFAULT_SIZE, 458, Short.MAX_VALUE)
                  .addGroup(jpBancoDadosLayout.createSequentialGroup()
                    .addGroup(jpBancoDadosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                      .addGroup(jpBancoDadosLayout.createSequentialGroup()
                        .addComponent(jtfUser, javax.swing.GroupLayout.PREFERRED_SIZE, 178, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED))
                      .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jpBancoDadosLayout.createSequentialGroup()
                        .addComponent(jLabel6)
                        .addGap(130, 130, 130)))
                    .addGroup(jpBancoDadosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                      .addComponent(jLabel7)
                      .addComponent(jpfPass, javax.swing.GroupLayout.PREFERRED_SIZE, 156, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGap(0, 0, Short.MAX_VALUE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)))
            .addComponent(jbBuscarLocal)
            .addContainerGap(22, Short.MAX_VALUE))))
    );
    jpBancoDadosLayout.setVerticalGroup(
      jpBancoDadosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jpBancoDadosLayout.createSequentialGroup()
        .addGap(0, 0, Short.MAX_VALUE)
        .addGroup(jpBancoDadosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
          .addComponent(jLabel1)
          .addComponent(jcbBase, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
        .addGroup(jpBancoDadosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
          .addComponent(jLabel2)
          .addComponent(jLabel3)
          .addComponent(jLabel4))
        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
        .addGroup(jpBancoDadosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
          .addComponent(jtfIp, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
          .addComponent(jtfPorta, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
          .addComponent(jtfNomeBd, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
        .addComponent(jLabel5)
        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
        .addGroup(jpBancoDadosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
          .addComponent(jtfLocal, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
          .addComponent(jbBuscarLocal))
        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
        .addGroup(jpBancoDadosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
          .addComponent(jLabel6)
          .addComponent(jLabel7))
        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
        .addGroup(jpBancoDadosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
          .addComponent(jtfUser, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
          .addComponent(jpfPass, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
        .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
    );

    jtpCfg.addTab("Banco de dados", jpBancoDados);

    jLabel8.setText("Local onde os arquivos .xml das Nf-e estão armazenados");

    jButton1.setText("Buscar");

    javax.swing.GroupLayout jpXmlLayout = new javax.swing.GroupLayout(jpXml);
    jpXml.setLayout(jpXmlLayout);
    jpXmlLayout.setHorizontalGroup(
      jpXmlLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addGroup(jpXmlLayout.createSequentialGroup()
        .addContainerGap()
        .addGroup(jpXmlLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
          .addComponent(jtfLocalXml)
          .addGroup(jpXmlLayout.createSequentialGroup()
            .addComponent(jLabel8)
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
            .addComponent(jButton1)))
        .addContainerGap(74, Short.MAX_VALUE))
    );
    jpXmlLayout.setVerticalGroup(
      jpXmlLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addGroup(jpXmlLayout.createSequentialGroup()
        .addContainerGap()
        .addGroup(jpXmlLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
          .addComponent(jLabel8)
          .addComponent(jButton1))
        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
        .addComponent(jtfLocalXml, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        .addContainerGap(220, Short.MAX_VALUE))
    );

    jtpCfg.addTab("Xml", jpXml);

    getContentPane().add(jtpCfg, java.awt.BorderLayout.CENTER);

    jbGravar.setText("Gravar");
    jbGravar.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        jbGravarActionPerformed(evt);
      }
    });

    jbCancelar.setText("Cancelar");
    jbCancelar.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        jbCancelarActionPerformed(evt);
      }
    });

    jbPadrao.setText("Padrão");
    jbPadrao.setEnabled(false);

    javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
    jPanel2.setLayout(jPanel2Layout);
    jPanel2Layout.setHorizontalGroup(
      jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addGroup(jPanel2Layout.createSequentialGroup()
        .addContainerGap()
        .addComponent(jbGravar)
        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
        .addComponent(jbCancelar)
        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
        .addComponent(jbPadrao)
        .addContainerGap(320, Short.MAX_VALUE))
    );

    jPanel2Layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {jbCancelar, jbGravar, jbPadrao});

    jPanel2Layout.setVerticalGroup(
      jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addGroup(jPanel2Layout.createSequentialGroup()
        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
          .addComponent(jbGravar)
          .addComponent(jbCancelar)
          .addComponent(jbPadrao))
        .addGap(0, 12, Short.MAX_VALUE))
    );

    getContentPane().add(jPanel2, java.awt.BorderLayout.SOUTH);

    pack();
  }// </editor-fold>//GEN-END:initComponents

  private void jbCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbCancelarActionPerformed
    dispose();
  }//GEN-LAST:event_jbCancelarActionPerformed

  private void jcbBaseActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jcbBaseActionPerformed
    switch (jcbBase.getSelectedIndex()) {
      case 0:
        jtfIp.setEnabled(true);
        jtfPorta.setEnabled(true);
        jtfLocal.setEnabled(false);
        break;
      case 1:
        jtfIp.setEnabled(false);
        jtfPorta.setEnabled(false);
        jtfLocal.setEnabled(true);
        break;
    }
  }//GEN-LAST:event_jcbBaseActionPerformed

  private void jbGravarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbGravarActionPerformed
    this.setCfg();
    this.dispose();
  }//GEN-LAST:event_jbGravarActionPerformed

  /**
   * @param args the command line arguments
   */
  public static void main(String args[]) {
    /* Set the Nimbus look and feel */
    //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
     * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
     */
    try {
      for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
        if ("Nimbus".equals(info.getName())) {
          javax.swing.UIManager.setLookAndFeel(info.getClassName());
          break;
        }
      }
    } catch (ClassNotFoundException ex) {
      java.util.logging.Logger.getLogger(CfgJDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (InstantiationException ex) {
      java.util.logging.Logger.getLogger(CfgJDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (IllegalAccessException ex) {
      java.util.logging.Logger.getLogger(CfgJDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (javax.swing.UnsupportedLookAndFeelException ex) {
      java.util.logging.Logger.getLogger(CfgJDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    }
    //</editor-fold>

    /* Create and display the dialog */
    java.awt.EventQueue.invokeLater(new Runnable() {
      @Override
      public void run() {
        CfgJDialog dialog = new CfgJDialog(new javax.swing.JFrame(), true);
        dialog.addWindowListener(new java.awt.event.WindowAdapter() {
          @Override
          public void windowClosing(java.awt.event.WindowEvent e) {
            System.exit(0);
          }
        });
        dialog.setVisible(true);
      }
    });
  }
  // Variables declaration - do not modify//GEN-BEGIN:variables
  private javax.swing.JButton jButton1;
  private javax.swing.JLabel jLabel1;
  private javax.swing.JLabel jLabel2;
  private javax.swing.JLabel jLabel3;
  private javax.swing.JLabel jLabel4;
  private javax.swing.JLabel jLabel5;
  private javax.swing.JLabel jLabel6;
  private javax.swing.JLabel jLabel7;
  private javax.swing.JLabel jLabel8;
  private javax.swing.JLabel jLabel9;
  private javax.swing.JPanel jPanel1;
  private javax.swing.JPanel jPanel2;
  private javax.swing.JButton jbBuscarLocal;
  private javax.swing.JButton jbCancelar;
  private javax.swing.JButton jbGravar;
  private javax.swing.JButton jbPadrao;
  private javax.swing.JComboBox jcbBase;
  private javax.swing.JPanel jpBancoDados;
  private javax.swing.JPanel jpXml;
  private javax.swing.JPasswordField jpfPass;
  private javax.swing.JTextField jtfIp;
  private javax.swing.JTextField jtfIpMixdb;
  private javax.swing.JTextField jtfLocal;
  private javax.swing.JTextField jtfLocalXml;
  private javax.swing.JTextField jtfNomeBd;
  private javax.swing.JTextField jtfPorta;
  private javax.swing.JTextField jtfUser;
  private javax.swing.JTabbedPane jtpCfg;
  // End of variables declaration//GEN-END:variables

  private void getCfg() {
    CfgPropUtil cfgProp = new CfgPropUtil();
    Cfg cfg = cfgProp.getCfgProp();

    switch (cfg.getBase()) {
      case Cfg.BASE_POSTGRESQL:
        jcbBase.setSelectedIndex(0);
        jtfIp.setEnabled(true);
        jtfPorta.setEnabled(true);
        jtfLocal.setEnabled(false);

        jtfIp.setText(cfg.getIp());
        jtfPorta.setText(cfg.getPorta());
        jtfNomeBd.setText(cfg.getNomeBd());
        jtfUser.setText(cfg.getUser());
        jpfPass.setText(cfg.getPassword());

        break;
      case Cfg.BASE_HSQLDB:
        jcbBase.setSelectedIndex(1);
        jtfIp.setEnabled(false);
        jtfPorta.setEnabled(false);
        jtfLocal.setEnabled(true);

        jtfLocal.setText(cfg.getLocal());
        jtfNomeBd.setText(cfg.getNomeBd());
        jtfUser.setText(cfg.getUser());
        jpfPass.setText(cfg.getPassword());
        break;
    }
    jtfLocalXml.setText(cfg.getXmlDiretorio());
    jtfIpMixdb.setText(cfg.getIpMixdb());
  }

  private void setCfg() {
    CfgPropUtil cfgProp = new CfgPropUtil();
    Cfg cfg = new Cfg();

    switch (jcbBase.getSelectedIndex()) {
      case 0:// PostgreSQL
        cfg.setBase(Cfg.BASE_POSTGRESQL);
        cfg.setIp(jtfIp.getText());
        cfg.setPorta(jtfPorta.getText());
        cfg.setNomeBd(jtfNomeBd.getText());
        cfg.setUser(jtfUser.getText());
//        cfg.setPassword(jpfPass.getPassword().toString());
        cfg.setPassword(Util.getPasswordJpf(jpfPass));
        break;
      case 1:// HSQLDB
        cfg.setBase(Cfg.BASE_HSQLDB);
        cfg.setLocal(jtfLocal.getText());
        cfg.setNomeBd(jtfNomeBd.getText());
        cfg.setUser(jtfUser.getText());
        cfg.setPassword(jpfPass.getPassword().toString());
        break;
    }
    cfg.setXmlDiretorio(jtfLocalXml.getText());
    cfg.setIpMixdb(jtfIpMixdb.getText());
    cfgProp.setCfgProp(cfg);
  }
}
